// pages/index/business/serviceDetails/index.js
import { serviceGet, getReserve } from  "../../../../utils/api/module/cyfw";
import { getLoginSendcode } from  "../../../../utils/api/module/login";
Page({
    data: {
        // 轮播图
        lunbotu:{
            gbConfig: getApp().globalData.config,
            background: [
                { pic: 'https://ythapi.bjyangshu.com/uploads/202012/31/202012311400098653.jpg'},
                { pic: 'https://ythapi.bjyangshu.com/uploads/202012/31/202012311400098653.jpg'},
                { pic: 'https://ythapi.bjyangshu.com/uploads/202012/31/202012311400098653.jpg'},
            ],
            indicatorDots: false,
            vertical: false,
            autoplay: true,
            interval: 3000,
            duration: 600,
            circular: true,
            swiperCurrent: 0
        },
        content: '<img src="https://ythapi.bjyangshu.com/uploads/202012/31/202012311400098653.jpg" />',
        subscribeType: 2, // 预约弹窗
        id: '',
        currentData: {}, // 当前数据
        messagecode: '', // 验证码
        position: '', // 职务
        companyname: '', // 公司
        phone: '', // 电话
        contact: '', // 联系人
        timerNumber: 60,
        timerFlag: false,
        codeText: '获取验证码'
    },
    onLoad: function (options) {
        this.setData({ gbConfig: getApp().globalData.config })
        this.data.id = options.id;
        this.getData();
    },
    getData: function() {
        serviceGet({
            id: this.data.id
        }).then( res => {
            this.setData({ currentData: res.data.data });
        })
    },
    /** l轮播图current设置 */
    swiperAnimationListener(e){
        this.setData({
            ["lunbotu.swiperCurrent"]: e.detail.current
        })
    },
    // 提交
    bindSeve: function() {
        if(!this.data.companyname) {
            wx.showToast({title: '公司名称不能为空', icon: 'none'});
            return false;
        };
        if(!this.data.position) {
            wx.showToast({title: '职务不能为空', icon: 'none'});
            return false;
        };
        if(!this.data.contact) {
            wx.showToast({title: '联系人不能为空', icon: 'none'});
            return false;
        };
        if(!this.data.phone) {
            wx.showToast({title: '电话号码不能为空', icon: 'none'});
            return false;
        };
        if(!(/^1[345678]\d{9}$/.test(this.data.phone))){
            wx.showToast({ title: "请输入正确的电话号码", icon: "none" });
            return false;
        };
        // if(!this.data.codeData){
        //     wx.showToast({ title: "验证码不能为空", icon: "none" });
        //     return false;
        // };
        // if(Number(this.data.messagecode) !== Number(this.data.codeData)) {
        //     wx.showToast({title: '验证码不正确', icon: 'none'});
        //     return false;
        // };
        this.closePup();
        getReserve({
            mid: wx.getStorageSync('userInfo').mid,
            sid: this.data.id,
            // messagecode: this.data.messagecode, // 验证码
            position: this.data.position, // 职务
            companyname: this.data.companyname, // 公司
            phone: this.data.phone, // 电话
            contact: this.data.contact, // 联系人
        },{
            Cookie: 'SESSION_='+wx.getStorageSync('session_id'),
        }).then(res => {
            if(res.data.code == 0) {
                wx.showToast({title: res.data.message, icon: 'none'});
                return false;
            }
            if(res.data.code == 1) {
                wx.navigateTo({
                    url: '/pages/index/business/succeed/index',
                });
            };
        });
    },
    input: function(e) {
        let name = e.currentTarget.dataset.name;
        this.setData({[name]: e.detail.value})
    },
    // 关闭弹窗
    closePup: function() {
        let userData = wx.getStorageSync('userInfoData');
        if(!userData.data || !userData.data.certstatus && userData.data.certstatus != 0) {
            wx.showModal({
                title: '提示',
                content: '必须登录且认证通过才能使用该功能',
                cancelText: '取消',
                cancelColor: '#999999',
                confirmText: '去登录',
                confirmColor: '#3b99f5',
                success: res => {
                  this.data.theRedEnvelopeSuper = false;
                  if (res.confirm) {
                    wx.navigateTo({
                        url: '/pages/loginAndRegister/login/index'
                    });
                  };
                }
            });
            return false;
        };
        this.setData({ subscribeType: this.data.subscribeType == 1 ? 2 : 1 });
    },
    getCode: function() {
        if(!this.data.phone){
            wx.showToast({ title: "请输入电话号", icon: "none" });
            return false;
        };
        if(!(/^1[345678]\d{9}$/.test(this.data.phone))){
            wx.showToast({ title: "请输入正确的电话号码", icon: "none" });
            return false;
        };
        if(this.data.timerFlag) {
            wx.showToast({title: '验证码已发送，请耐心等待！', icon: 'none'});
            return false;
        };
        this.setData({ timerFlag: true, codeText: '60秒后重试'});
        this.getTimer();
        getLoginSendcode({
            mobile: this.data.phone
        }).then(res => {
            if(res.data.code == 0) {
                wx.showToast({title: res.data.message, icon: 'none'});
                return false;
            };
            if(res.data.code == 1) {
                this.data.codeData = res.data.data;
            };
        });
    },
    // 倒计时
    getTimer: function() {
        this.data.timerFunction = setInterval(() => {
            if(this.data.timerNumber - 1>= 0) {
                this.data.timerNumber -= 1; 
                this.setData({ codeText: this.data.timerNumber + '秒后重试' });
            }else{
                clearInterval(this.data.timerFunction)
                this.setData({timerFlag: false, codeText: '重新发送' })
            };
        }, 1000)
    },
    onHide: function() {
        clearInterval(this.data.timerFunction)
    },
    onUnload: function() {
        clearInterval(this.data.timerFunction)
    }
})